package top.lyc829.zzl_android.ui.setting;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;

import com.bumptech.glide.Glide;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.xuexiang.xui.utils.DensityUtils;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xui.widget.grouplist.XUICommonListItemView;
import com.xuexiang.xui.widget.grouplist.XUIGroupListView;

import java.util.HashMap;
import java.util.Map;

import top.lyc829.zzl_android.R;
import top.lyc829.zzl_android.databinding.ActivitySettingBinding;
import top.lyc829.zzl_android.logic.bean.LiveEventMsg;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.ui.BaseActivity;
import top.lyc829.zzl_android.ui.index.serach.SearchDetial;

public class SettingActivity extends BaseActivity {

    ActivitySettingBinding binding;

    XUIGroupListView mSettingGroupListView;
    XUICommonListItemView itemNotice, itemHelpAndFeedback, itemAgreement, itemAbout, itemLogout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivitySettingBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        // 点击返回按钮返回上一页
        binding.mTitleBarSetting.setLeftClickListener(v -> {
           finish();
        });

        mSettingGroupListView = findViewById(R.id.settingGroupListView);


        initGroupListView();



    }

    private void initGroupListView() {
        String notice=Utils.getValueFromSp("notice");


        //itemNotice = mSettingGroupListView.createItemView("通知设置");
        //itemNotice.setAccessoryType(XUICommonListItemView.ACCESSORY_TYPE_CHEVRON);

        XUICommonListItemView itemNotice = mSettingGroupListView.createItemView("通知设置");
        itemNotice.setAccessoryType(XUICommonListItemView.ACCESSORY_TYPE_SWITCH);

        if(notice.equals("0"))
        {
            itemNotice.getSwitch().setChecked(false);
        }
        else
        {
            itemNotice.getSwitch().setChecked(true);
        }
        itemNotice.getSwitch().setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                {
                    Utils.insertToSp("notice","1");
                }
                else{
                    Utils.insertToSp("notice","0");
                }
            }
        });



        itemHelpAndFeedback = mSettingGroupListView.createItemView("帮助与反馈");
        itemHelpAndFeedback.setAccessoryType(XUICommonListItemView.ACCESSORY_TYPE_CHEVRON);

        itemAgreement = mSettingGroupListView.createItemView("用户协议");
        itemAgreement.setAccessoryType(XUICommonListItemView.ACCESSORY_TYPE_CHEVRON);

        itemAbout = mSettingGroupListView.createItemView("关于");
        itemAbout.setAccessoryType(XUICommonListItemView.ACCESSORY_TYPE_CHEVRON);

        itemLogout = mSettingGroupListView.createItemView("退出登录");
        itemLogout.setAccessoryType(XUICommonListItemView.ACCESSORY_TYPE_CHEVRON);



        View.OnClickListener onClickListener = v -> {
            if (v instanceof XUICommonListItemView) {
                String text = (String) ((XUICommonListItemView) v).getText();
                switch (text) {
                    case "通知设置":
                        break;
                    case "帮助与反馈":
                        Intent intent1 = new Intent(this, BackActivity.class);
                        startActivity(intent1);
                        break;
                    case "用户协议":
                        new MaterialDialog.Builder(this)
                                .title("用户协议")
                                .content(R.string.user_agreement_detail)
                                .positiveText("关闭")
                                .show();
                        break;
                    case "关于":
                        Intent intent = new Intent(this, about.class);
                        startActivity(intent);
                        break;
                    case "退出登录":
                        logout();
                        break;
                    default:
                        break;
                }

            }
        };

        int size = DensityUtils.dp2px(this, 20);
        XUIGroupListView.newSection(this)
                .setLeftIconSize(size, ViewGroup.LayoutParams.WRAP_CONTENT)
                .addItemView(itemNotice, onClickListener)
                .addItemView(itemHelpAndFeedback, onClickListener)
                .addItemView(itemAgreement, onClickListener)
                .addItemView(itemAbout, onClickListener)

                .addTo(mSettingGroupListView);

        XUIGroupListView.newSection(this)
                .addItemView(itemLogout, onClickListener)
                .addTo(mSettingGroupListView);

    }

    /**
     * 退出登录，清除token
     */
    private void logout() {
        if (Utils.isLogin()) {
            insertToSp("token", "");
            insertToSp("user_id", "");
            showToast("退出登录成功");

            sendLiveEvent();


            finish();
        } else {
            showToast("你还未登录");
        }

    }

    private void sendLiveEvent() {

//        Map<String, String> map = new HashMap<>();
//        map.put("logout", "1");

        LiveEventBus
                .get("logout")
                .post(1);
    }


}